package com.zm.Study01_FileOutputStreamTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo02 {

    FileOutputStream fos=new FileOutputStream("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\FileOutputStreamTxt\\fos.txt");

    public FileOutputStreamDemo02() throws FileNotFoundException {
    }

    //构造演化
    public void demo01() throws FileNotFoundException {
        //演化
        File file=new File("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\fos.txt");
        FileOutputStream fos01=new FileOutputStream(file);

        FileOutputStream fos02=new FileOutputStream(new File("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\fos.txt"));

        FileOutputStream fos=new FileOutputStream("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\fos.txt");
    }



    public static void main(String[] args) throws IOException {
        FileOutputStreamDemo02 fileOutputStreamDemo02=new FileOutputStreamDemo02();
       // fileOutputStreamDemo02.fosWriteInt();
       // fileOutputStreamDemo02.fosWriteBytes();
       // fileOutputStreamDemo02.fosWritegetBytes("abcdefg");
       // fileOutputStreamDemo02.fosWriteBytesPart("12345",1,3); //234
       // fileOutputStreamDemo02.fosWriteEnter("hello");
        fileOutputStreamDemo02.fosWriteAppend("hello");//fos2.txt


    }
    //write(Int a) //abc
    public void fosWriteInt() throws IOException {
        fos.write(97);//a
        fos.write(98);//b
        fos.write(99);//c
        fos.close();
    }
    //write(byte[] bytes) //def
    public void fosWriteBytes() throws IOException {
       byte[] bytes={100,101,102};//d,e,f
       fos.write(bytes);
       fos.close();
    }
    //write(byte[] bytes) bytes=str.getBytes()
    public void fosWritegetBytes(String s) throws IOException {
        byte[] bytes=s.getBytes();

        fos.write(bytes);
        fos.close();

    }
    //write(byte[] bytes,int off,int len) //12345->234
    public void fosWriteBytesPart(String s,int start,int len) throws IOException {
        byte[] bytes=s.getBytes();
        fos.write(bytes,start,len);
        fos.close();
    }
    //换行 "\n".getBytes()
    /*
    字节流写数据换行
    windows:\r\n
    linux:\n
    mac:\r
    idea是可以识别多种换行，所以直接去路径找文件才能看到真实效果
     */
    public void fosWriteEnter(String s) throws IOException {
        byte[] bytes=s.getBytes();
        for (int i=0;i<10;i++){
            fos.write(bytes);
            fos.write("\n".getBytes());
        }
        fos.close();
    }

    //追加写入FileOutputStream fileOutputStream=new FileOutputStream("文件路径以及文件名",true);开启追加写入
    public void fosWriteAppend(String s) throws IOException {
       FileOutputStream fileOutputStream=new FileOutputStream("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\FileOutputStreamTxt\\fos2.txt",true);
        byte[] bytes=s.getBytes();
        for (int i=0;i<10;i++){
            fileOutputStream.write(bytes);
            fileOutputStream.write("\r\n".getBytes());
        }
        fileOutputStream.write("一次循环\r\n".getBytes());
        fileOutputStream.close();
    }
}
